package com.webservice.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.apache.log4j.Logger;

/**
 * xml文件的校验
 * 
 * @author Andy
 * 
 */
public class XmlCheck {

	 private static Logger logger = Logger.getLogger(XmlCheck.class);

	private static final String PROPERTIES_FILE_PATH = "xsd";
	
	private static final String PROPERTIES_FILE_NAME = ".xsd";


	public static String valid(String billtype, String xml) throws IOException {
		String path = XmlCheck.class.getResource("/").getPath();
		int index = path.indexOf(":");
		path = path.substring(index >= 0 ? 1 : 0, path.indexOf("classes"));
		path+=PROPERTIES_FILE_PATH + System.getProperty("file.separator") +billtype+ PROPERTIES_FILE_NAME;
		logger.debug(path);
		//"E://tytelling//PubWebService//WebContent//WEB-INF//xsd//B1001.xsd"
		InputStream is = new BufferedInputStream(new FileInputStream(path));
		//E:\tytelling\PubWebService\WebContent\WEB-INF\xsd\B1001.xsd
		
		ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
		SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
		try {
//			InputStream input=new InputStream(new File("E://tytelling//PubWebService//WebContent//WEB-INF//xsd//B1001.xsd"));
			Source sourceXSD = new StreamSource(is);
			Schema schema = schemaFactory.newSchema(sourceXSD);
			Validator validator = schema.newValidator();
			Source source = new StreamSource(inputStream);
			validator.validate(source);
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();	
		}
	}

	public static void main(String[] args) throws IOException {
		String xml =
		"<?xml version=\"1.0\" encoding=\"UTF-8\"?>              "+
		"<factory>                                           "+
		"  <intocode>201312310020</intocode>                 "+
		"  <storecode>201312200002</storecode>               "+
		"  <intodate>2013-12-31 14:31:00</intodate>          "+
		"  <contact></contact>                               "+
		"  <phone>18655519805</phone>                        "+
		"  <remark></remark>                                 "+
		"  <products>                                        "+
		"    <product>                                       "+
		"      <productcode>17001</productcode>              "+
		"      <productname>诺基亚1050 黑色</productname>    "+
		"      <quantity>60</quantity>                       "+
		"      <supplyid>176</supplyid>                      "+
		"      <serials>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000002</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000001</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000004</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000003</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000010</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000009</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000006</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000005</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000008</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000007</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000015</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000014</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000013</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000012</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000011</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000020</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000019</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000018</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000017</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000016</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000027</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000028</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000029</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000030</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000021</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000022</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000023</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000024</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000025</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000026</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000040</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000038</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000039</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000032</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000033</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000031</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000036</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000037</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000034</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000035</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000050</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000049</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000045</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000046</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000047</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000048</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000041</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000042</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000043</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000044</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000051</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000058</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000059</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000056</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000057</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000054</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000055</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000052</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000060</imei>              "+
		"        </seri>                                     "+
		"        <seri>                                      "+
		"          <imei>424564000000053</imei>              "+
		"        </seri>                                     "+
		"      </serials>                                    "+
		"    </product>                                      "+
		"  </products>                                       "+
		"</factory>                                          ";
		System.out.println(xml);
		System.out.println(valid("F0001", xml));
//		System.out.println(valid("B1001", xml));
	}
}
